
/*
 * Copyright 2008 Friendster Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
 
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using Friendster.Service.Core;
using System.Net;

namespace Friendster.Service
{
    /// <summary>
    /// This class includes all methods related to friends supported by Friendster Developer platform
    /// </summary>
    public sealed class FriendsService
    {
        // Fields
        private string _session;

        // Methods
        internal FriendsService(string session)
        {
            this._session = session;
        }

        /// <summary>
        /// Check for, whether the given UID's are friends or not
        /// </summary>
        /// <param name="userID1"></param>
        /// <param name="userID2"></param>
        /// <returns>Boolean Value</returns>
        public bool AreFriends(string userID1, string userID2)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            if (string.IsNullOrEmpty(userID1) || string.IsNullOrEmpty(userID2))
            {
                userID2 = string.IsNullOrEmpty(userID1) ? userID2 : userID1;
                userID1 = request.UserID;
            }

            FriendsterResponse response = request.InvokeMethod("GET", "arefriends/" + userID1 + "," + userID2);
            if (!response.IsError)
            {
                ArrayList result = (ArrayList)response.Result;
                Hashtable depthTable = (Hashtable)result[0];
                return (bool)(depthTable["are_friends"]);
            }
            return false;
        }
        /// <summary>
        /// Check for, whether the given UID is a friend with logged in user - or not.
        /// </summary>
        /// <param name="userID"></param>
        /// <returns>Booloean Value</returns>
        public bool AreFriends(string userID)
        {
            return AreFriends(null, userID);
        }
        
        /// <summary>
        /// Returns friendship depth level between given two UID's
        /// </summary>
        /// <param name="userID1"></param>
        /// <param name="userID2"></param>
        /// <returns>Int value of friendship level</returns>
        public int FriendDepth(string userID1, string userID2)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            if (string.IsNullOrEmpty(userID1) || string.IsNullOrEmpty(userID2))
            {
                userID2 = string.IsNullOrEmpty(userID1) ? userID2 : userID1;
                userID1 = request.UserID;
            }

            FriendsterResponse response = request.InvokeMethod("GET", "depth/" + userID1 + "," + userID2);
            if (!response.IsError)
            {
                ArrayList result = (ArrayList)response.Result;
                Hashtable depthTable = (Hashtable)result[0];
                return (int)depthTable["depth"];
            }
            return 0;
        }
        /// <summary>
        /// Returns friendship depth level between loggedin user and given UID
        /// </summary>
        /// <param name="userID"></param>
        /// <returns>Int value of friendship level</returns>
        public int FriendDepth(string userID)
        {
            return FriendDepth(null, userID);
        }
        /// <summary>
        /// Returns the list of all friends for given UID
        /// </summary>
        /// <param name="userID"></param>
        /// <returns>String array of UID's</returns>
        public string[] GetFriends(string userID)
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            if (string.IsNullOrEmpty(userID))
            {
                userID = request.UserID;
            }

            FriendsterResponse response = request.InvokeMethod("GET", "friends/" + userID);
            if (!response.IsError)
            {
                ArrayList _result = (ArrayList)response.Result;

                Hashtable result = (Hashtable)_result[0];
                ArrayList friendlist = new ArrayList();
                friendlist = (ArrayList)result["friends"];

                return (string[])friendlist.ToArray(typeof(string));
            }
            return null;
        }
        /// <summary>
        /// Return the list of all friends for current loggedin user.
        /// </summary>
        /// <returns>String array of UID's</returns>
        public string[] GetFriends()
        {
            return GetFriends(null);
        }
        /// <summary>
        /// Returns a list of user ids for friends who have installed this application.
        /// </summary>
        /// <returns></returns>
        public string[] AppFriends()
        {
            FriendsterRequest request = new FriendsterRequest(this._session);
            FriendsterResponse response = request.InvokeMethod("GET", "application/friends/");
            if (!response.IsError)
            {
                ArrayList _result = (ArrayList)response.Result;

                Hashtable result = (Hashtable)_result[0];
                ArrayList friendlist = new ArrayList();
                friendlist = (ArrayList)result["appfriends"];
                if (friendlist!=null)
                    return (string[])friendlist.ToArray(typeof(string));
            }
            return null;
        }
    }
}
